package com.guigu.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.MD5;
import com.guigu.common.utils.R;
import com.guigu.userservice.entity.Businessman;
import com.guigu.userservice.mapper.BusinessmanMapper;
import com.guigu.userservice.service.BusinessmanService;
import com.guigu.userservice.vo.BusinessmanVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 业务员表 服务实现类
 * </p>
 *
 * @author zcy
 * @since 2025-10-14
 */
@Service
public class BusinessmanServiceImpl extends ServiceImpl<BusinessmanMapper, Businessman> implements BusinessmanService {
    @Override
    public R queryBusinessmanPageByVo(int pageNum, int pageSize, BusinessmanVo businessmanVo) {
        QueryWrapper<Businessman> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(businessmanVo.getKeyword()!=null, "name", businessmanVo.getKeyword());
        List<Businessman> businessmanList = this.list(queryWrapper);
        PageInfo<Businessman> pageInfo = new PageInfo<>(businessmanList);
        return R.ok().data("pageInfo", pageInfo);
    }

    @Override
    public R updateStatus(Integer id, Integer status) {
        Businessman businessman = new Businessman();
        businessman.setBusinessmanId(id);
        businessman.setStatus(status);
        this.updateById(businessman);
        return R.ok();
    }

    @Override
    public R queryBusinessmanByVo(BusinessmanVo businessmanVo) {
        QueryWrapper<Businessman> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(businessmanVo.getKeyword()!=null, "name", businessmanVo.getKeyword());
        List<Businessman> businessmanList = this.list(queryWrapper);
        return R.ok().data("list", businessmanList);
    }

    @Override
    public R passWordChange(Integer userId, String newPassword) {
        newPassword= MD5.encrypt(newPassword);
        Businessman businessman = this.getById(userId);
        if (businessman == null) {
            return R.error().message("用户不存在");
        }
        businessman.setPassword(newPassword);
        this.updateById(businessman);
        return R.ok().message("修改成功");
    }
}
